﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CreatePGSQLDataScript
{
    public partial class ProgramTimeFlow : Form
    {
        public ProgramTimeFlow(string _currDiagram)
        {
            InitializeComponent();
            currDiagram = _currDiagram;
        }
        DataAccess.SqlProvider db;

        HashSet<string> hsModule = new HashSet<string>();
        HashSet<string> hsFunction = new HashSet<string>();

        string currDiagram = "";

        private void txtRemark_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddItem("",txtModule.Text, txtFunction.Text, txtCommand.Text, txtRemark.Text);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems == null)
                return;
            listView1.Items.Remove(listView1.SelectedItems[0]);

        }



        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count <=0)
                return;
            listView1.SelectedItems[0].SubItems[1].Text = txtModule.Text.Trim();
            listView1.SelectedItems[0].SubItems[2].Text = txtFunction.Text.Trim();
            listView1.SelectedItems[0].SubItems[3].Text = txtCommand.Text.Trim();
            listView1.SelectedItems[0].SubItems[4].Text = txtRemark.Text.Trim();
        }

        private void ProgramTimeFlow_Load(object sender, EventArgs e)
        {
            hsModule = new HashSet<string>();
            hsFunction = new HashSet<string>();
            
            db = new DataAccess.SqlProvider(OisLibrary.DB.Connection);

            LoadView(currDiagram);

        }

        private void AddItem(string color,params object[] data)
        {
            ListViewItem item = new ListViewItem();
            for (int i = 0; i < data.Length; i++)
            {
                item.SubItems.Add(data[i].ToString());
            }

            if(listView1.SelectedItems.Count>0)
            {
                int index=listView1.SelectedIndices[0];
                listView1.Items.Insert(index, item);
            }
            else
            {
                listView1.Items.Add(item);
            }

            if (color != "")
                item.ForeColor = Color.FromArgb(Convert.ToInt32(color));
           
            hsModule.Add(data[0].ToString());
            hsFunction.Add(data[1].ToString());

            txtModule.DataSource = hsModule.ToList();
            txtFunction.DataSource = hsFunction.ToList();

            txtModule.Text = data[0].ToString();
            txtFunction.Text = data[1].ToString();
        }


        private void LoadView(string diagram)
        {
            DataTable dt = db.GetDataTable(string.Format("select * from ProgramTimeOrder where diagram='{0}';", diagram));

            foreach (DataRow dr in dt.Rows)
            {
                AddItem(
                    dr["color"].ToString(),
                    dr["Module"].ToString(),
                    dr["FunName"].ToString(),
                    dr["command"].ToString(),
                    dr["Remark"].ToString()
                    
                    );
            }



        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("确认更新吗?", "提示", MessageBoxButtons.OKCancel);

            if (result == DialogResult.Cancel)
            {
                return;
            }

            StringBuilder sbSqlColl = new StringBuilder();

            sbSqlColl.Append(@"
begin transaction
begin try 
");


            sbSqlColl.Append(string.Format("delete ProgramTimeOrder where diagram='{0}';", currDiagram));

            string sqlTextTemp = "insert into ProgramTimeOrder values('{0}','{1}','{2}','{3}','{4}','{5}','{6}');";

            for (int i = 0; i < listView1.Items.Count; i++)
            {
                ListViewItem coll = listView1.Items[i];
                string module = coll.SubItems[1].Text;
                string funName = coll.SubItems[2].Text;
                string command = coll.SubItems[3].Text;
                string remark = coll.SubItems[4].Text;

                string sqlText = string.Format(sqlTextTemp, currDiagram, i.ToString(), module, funName,command, remark,coll.ForeColor.ToArgb());
                sbSqlColl.Append(sqlText);
            }

            sbSqlColl.Append(@"

  if @@TRANCOUNT >0
	commit;
end try
begin catch
  if @@TRANCOUNT >0
	rollback;
end catch
");

            db.ExecuteSQL(sbSqlColl.ToString());



        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count <= 0)
                return;
            txtModule.Text = listView1.SelectedItems[0].SubItems[1].Text;
            txtFunction.Text = listView1.SelectedItems[0].SubItems[2].Text;
            txtCommand.Text = listView1.SelectedItems[0].SubItems[3].Text;
            txtRemark.Text = listView1.SelectedItems[0].SubItems[4].Text;

        }

        private void listView1_Click(object sender, EventArgs e)
        {

        }

        private void btnBlank_Click(object sender, EventArgs e)
        {
            AddItem("","", "", "", "");
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count <= 0)
                return;

            int index = listView1.SelectedIndices[0];

            if (index <= 0)
                return;

            ListViewItem item1 = listView1.SelectedItems[0];

            listView1.Items.Remove(item1);

            listView1.Items.Insert(index - 1, item1);
            listView1.Items[index - 1].Selected = true; 
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if(listView1.SelectedItems.Count<=0)
                return;

            int index=listView1.SelectedIndices[0];

            if (index >= listView1.Items.Count-1)
                return;

            ListViewItem item1 =listView1.SelectedItems[0];

            listView1.Items.Remove(item1);

            listView1.Items.Insert(index+1, item1);
            listView1.Items[index + 1].Selected = true; 
        }

        private void btnSameColor_Click(object sender, EventArgs e)
        {
            //listView1.ForeColor = Color.Black;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView1.Items[i].SubItems[1].Text == txtModule.Text)
                {
                    listView1.Items[i].ForeColor = Color.SkyBlue;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(listView1.SelectedItems.Count<=0)
                return;


            colorDialog1.ShowDialog();

            foreach (ListViewItem item in listView1.SelectedItems)
            {
                item.ForeColor = colorDialog1.Color;
            }
        }

    }
}
